package com.sina.finance.api.framework.service.impl;

import com.sina.finance.api.framework.constants.Constants;
import com.sina.finance.api.framework.response.Response;
import com.sina.finance.api.framework.service.AppKeyAndSecretService;
import com.sina.finance.api.framework.service.BizIdAndAppKeyService;
import com.sina.finance.api.framework.service.IErrorResponseService;
import com.sina.finance.api.framework.service.ISecurityService;
import com.sina.finance.api.framework.utils.SignUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class SecurityServiceImpl implements ISecurityService {

	private static final String APP_KEY = "APP_KEY";

	private static final String APP_SECRET = "APP_SECRET";

	protected IErrorResponseService errorResponseService;

	@Resource
	private AppKeyAndSecretService appKeyAndSecretService;

	@Resource
	private BizIdAndAppKeyService bizIdAndAppKeyService;

	public IErrorResponseService getErrorResponseService() {
		return errorResponseService;
	}

	public void setErrorResponseService(IErrorResponseService errorResponseService) {
		this.errorResponseService = errorResponseService;
	}

	@Override
	public Response verifyRequest(Map<String, String[]> requestParams){

		Response response = null;

/*

			// 如果请求中不包含任何参数
			if(this.hasNoParam(requestParams)){
				return errorResponseService.getErrorResponse(ErrorCodeConstants.SYS_ERROR_CODE_S1);
			}

			// 如果没有传递app_key
			if(!this.hasParam(requestParams, Constants.SYS_PARAM_APP_KEY)){
				return errorResponseService.getErrorResponse(ErrorCodeConstants.SYS_ERROR_CODE_S2);
			}

			String appKey = requestParams.get(Constants.SYS_PARAM_APP_KEY)[0];

			// 如果app_key不存在
			if(!this.verifyAppKey(appKey)){
				return errorResponseService.getErrorResponse(ErrorCodeConstants.SYS_ERROR_CODE_S3);
			}
*/

/*
			// 如果没有传递method
			if(!this.hasParam(requestParams, Constants.SYS_PARAM_METHOD)){
				return errorResponseService.getErrorResponse(ErrorCodeConstants.SYS_ERROR_CODE_S4);
			}

			// 如果没有传递format
			if(!this.hasParam(requestParams, Constants.SYS_PARAM_FORMAT)){
				return errorResponseService.getErrorResponse(ErrorCodeConstants.SYS_ERROR_CODE_S5);
			}

			// 如果没有传递sign
			if(!this.hasParam(requestParams, Constants.SYS_PARAM_SIGN)){
				return errorResponseService.getErrorResponse(ErrorCodeConstants.SYS_ERROR_CODE_S7);
			}

			// 如果签名验证无效
			String secret = appKeyAndSecretService.getSecret(appKey);
			if(!this.checkSign(requestParams, secret)){
				return errorResponseService.getErrorResponse(ErrorCodeConstants.SYS_ERROR_CODE_S11);
			}
*/

		return response;
	}

	/**
	 * 判断请求中是否包含参数
	 *
	 * @param requestParams
	 * @return
	 */
	private boolean hasNoParam(Map<String, String[]> requestParams){

		boolean checkFlag = false;

		// 请求中没有包含任何参数
		if(requestParams == null || requestParams.isEmpty()){
			checkFlag = true;
		}

		return checkFlag;
	}

	/**
	 * 验证签名是否有效
	 *
	 * @param requestParams
	 * @param clientDigest
	 *@param secret  @return
	 * @throws IOException
	 */
	@Override
	public boolean checkSign(Map<String, String[]> requestParams, String clientDigest, String secret) throws IOException{

		boolean checkFlag = false;

		// 将去除sign的请求参数签名
		Map<String, String[]> signParams = new HashMap<String, String[]>();
		signParams.putAll(requestParams);
		signParams.remove(Constants.SYS_PARAM_SIGN);

		String checkSign = SignUtils.signRequest(signParams, secret);

		// 如果签名结果与请求中的签名相等，则验证通过
		if(clientDigest.equals(checkSign)){
			checkFlag = true;
		}

		return checkFlag;
	}

	/**
	 * 根据key检查请求中是否包含某特定参数
	 *
	 * @param requestParams
	 * @param key
	 * @return
	 */
	protected boolean hasParam(Map<String, String[]> requestParams, String key){

		boolean checkFlag = false;

		if(requestParams.containsKey(key) && requestParams.get(key) != null && !"".equals(requestParams.get(key))){
			checkFlag = true;
		}

		return checkFlag;
	}

	/**
	 * 验证APP_KEY是否有效
	 *
	 * @param appKey
	 * @return
	 */
	private boolean verifyAppKey(String appKey){

		return appKeyAndSecretService.isAppKeyExist(appKey);
	}


}
